﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        bool _initialized = false;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void label_message_ContextMenuClosing(object sender, ContextMenuEventArgs e)
        {

        }

        private void button_collect_Click(object sender, RoutedEventArgs e)
        {
            if(!_initialized)
            {
                //
                label_message.Content = "请打开串口!";
                return;
            }
            label_message.Content = "串口已打开!";
            double current_value = 0.0;
            int total_value = 70;
            int lower_value = -10;

            Random rand = new Random();
            current_value = rand.Next(lower_value,total_value);

            
            double temperature = (current_value - 4) / 16 * total_value + lower_value;
            label_message.Content = "串口已打开!当前温度是" + temperature + "℃";

            double temp_limit = Convert.ToDouble(temperature_limit.Text);
            if(temperature > temp_limit)
            {
                label_warning.Content = "温度太高!";
            }
            else
            {
                label_warning.Content = "一切正常!";
            }
        }

        private void button_open_Click(object sender, RoutedEventArgs e)
        {
            //open serial port
            _initialized = true;
        }

        private void temperatureLimit_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void temperature_limit_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
